﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace MC34063_Universal_Calculator
{
    public class InputData :  INotifyPropertyChanged
    {
        bool _isUpDown = false;

        public bool IsUpDown
        {
            get { return _isUpDown; }
            set { _isUpDown = value; NotifyPropertyChanged("IsUpDown"); }
        }
        public delegate void UpDownSwitchHandler(object sender, bool isUpDown);

        public event UpDownSwitchHandler UpDownSwitch;

        public void OnUpDownSwitch(bool isUpDown)
        {
            if (UpDownSwitch != null)
                UpDownSwitch(this, isUpDown);
        }

        uint _fmax;

        public uint Fmax
        {
            get { return _fmax; }
            set { _fmax = value; NotifyPropertyChanged("Fmax"); }
        }

        ObservableCollection<ICValue> _icValues;

        public ObservableCollection<ICValue> IcValues
        {
            get { return _icValues; }
            set { _icValues = value; NotifyPropertyChanged("IcValues"); }
        }
        ICValue _selectedICValue;

        public ICValue SelectedICValue
        {
            get { return _selectedICValue; }
            set { 
                _selectedICValue = value; 
                NotifyPropertyChanged("SelectedICValue"); 
                string nameLocalized;
                WPFLocalizeExtension.Extensions.LocTextExtension locTextExt = new WPFLocalizeExtension.Extensions.LocTextExtension("MC34063_Universal_Calculator:Global:Andere");
                locTextExt.ResolveLocalizedValue(out nameLocalized);

                if (value.Name == nameLocalized)
                {
                    VsatEnabled = true;
                    VrefEnabled = true;
                    VrefAccuracyEnabled = false;
                }
                else
                {
                    VsatEnabled = true;
                    VrefEnabled = false;
                    VrefAccuracyEnabled = false;
                }

                Fmax = value.MaxFrequency;
                Vsat = value.Vsat;
                Vref = value.Vref;
                VrefAccuracy = value.Accuracy;
                Tmax = value.Tmax;
                Tmin = value.Tmin;
            }
        }
        int _Tmin;

        public int Tmin
        {
            get { return _Tmin; }
            set { _Tmin = value; NotifyPropertyChanged("Tmin"); }
        }
        int _Tmax;

        public int Tmax
        {
            get { return _Tmax; }
            set { _Tmax = value; NotifyPropertyChanged("Tmax"); }
        }
        double _Vref;

        public double Vref
        {
            get { return _Vref; }
            set { _Vref = value; NotifyPropertyChanged("Vref"); }
        }
        bool _VrefEnabled;

        public bool VrefEnabled
        {
            get { return _VrefEnabled; }
            set { _VrefEnabled = value; NotifyPropertyChanged("VrefEnabled"); }
        }

        double _VrefAccuracy;

        public double VrefAccuracy
        {
            get { return _VrefAccuracy; }
            set { _VrefAccuracy = value; NotifyPropertyChanged("VrefAccuracy"); }
        }
        bool _VrefAccuracyEnabled;

        public bool VrefAccuracyEnabled
        {
            get { return _VrefAccuracyEnabled; }
            set { _VrefAccuracyEnabled = value; NotifyPropertyChanged("VrefAccuracyEnabled"); }
        }

        double _Vinmin;

        public double Vinmin
        {
            get { return _Vinmin; }
            set 
            {
                _Vinmin = value; 
                
                if (Vout < Vinmax && Vout > Vinmin)
                {
                    OnUpDownSwitch(true);
                    IsUpDown = true;
                }
                else if (_isUpDown)
                {
                    OnUpDownSwitch(false);
                    IsUpDown = false;
                }
                NotifyPropertyChanged("Vinmin");
            }
        }
        double _Vinmax;

        public double Vinmax
        {
            get { return _Vinmax; }
            set 
            { 
                _Vinmax = value; 
                
                if (Vout < Vinmax && Vout > Vinmin)
                {
                    OnUpDownSwitch(true);
                    IsUpDown = true;
                }
                else if (_isUpDown)
                {
                    OnUpDownSwitch(false);
                    IsUpDown = false;
                }
                NotifyPropertyChanged("Vinmax");
            }
        }
        double _Vout;

        public double Vout
        {
            get { return _Vout; }
            set 
            {
                _Vout = value; 
                
                if (Vout < Vinmax && Vout > Vinmin)
                {
                    OnUpDownSwitch(true);
                    IsUpDown = true;
                }
                else if (_isUpDown)
                {
                    OnUpDownSwitch(false);
                    IsUpDown = false;
                }
                NotifyPropertyChanged("Vout");
            }
        }
        uint _frequency;

        public uint Frequency
        {
            get { return _frequency; }
            set { _frequency = value; NotifyPropertyChanged("Frequency"); }
        }
        double _Ioutmax;

        public double Ioutmax
        {
            get { return _Ioutmax; }
            set { _Ioutmax = value; NotifyPropertyChanged("Ioutmax"); }
        }
        double _Vripplepp;

        public double Vripplepp
        {
            get { return _Vripplepp; }
            set { _Vripplepp = value; NotifyPropertyChanged("Vripplepp"); }
        }
        bool _VsatEnabled;

        public bool VsatEnabled
        {
            get { return _VsatEnabled; }
            set { _VsatEnabled = value; NotifyPropertyChanged("VsatEnabled"); }
        }

        double _Vsat;

        public double Vsat
        {
            get { return _Vsat; }
            set { _Vsat = value; NotifyPropertyChanged("Vsat"); }
        }
        double _Vsat2;

        public double Vsat2
        {
            get { return _Vsat2; }
            set { _Vsat2 = value; NotifyPropertyChanged("Vsat2"); }
        }
        ObservableCollection<DiodeValue> _diodeValues;

        public ObservableCollection<DiodeValue> DiodeValues
        {
            get { return _diodeValues; }
            set { _diodeValues = value; NotifyPropertyChanged("DiodeValues"); }
        }
        DiodeValue _selectedDiodeValue;

        public DiodeValue SelectedDiodeValue
        {
            get { return _selectedDiodeValue; }
            set {
                string nameLocalized;
                WPFLocalizeExtension.Extensions.LocTextExtension locTextExt = new WPFLocalizeExtension.Extensions.LocTextExtension("MC34063_Universal_Calculator:Global:Andere");
                locTextExt.ResolveLocalizedValue(out nameLocalized);

                _selectedDiodeValue = value; 
                NotifyPropertyChanged("SelectedDiodeValue");

                if (value.Name == nameLocalized)
                    DiodeVfEnabled = true;
                else
                    DiodeVfEnabled = false;

                DiodeVf = value.Vf;
            }
        }
        DiodeValue _selectedDiode2Value;

        public DiodeValue SelectedDiode2Value
        {
            get { return _selectedDiode2Value; }
            set
            {
                string nameLocalized;
                WPFLocalizeExtension.Extensions.LocTextExtension locTextExt = new WPFLocalizeExtension.Extensions.LocTextExtension("MC34063_Universal_Calculator:Global:Andere");
                locTextExt.ResolveLocalizedValue(out nameLocalized);

                _selectedDiode2Value = value;
                NotifyPropertyChanged("SelectedDiode2Value");

                if (value.Name == nameLocalized)
                    Diode2VfEnabled = true;
                else
                    Diode2VfEnabled = false;

                Diode2Vf = value.Vf;
            }
        }
        bool _DiodeVfEnabled;

        public bool DiodeVfEnabled
        {
            get { return _DiodeVfEnabled; }
            set { _DiodeVfEnabled = value; NotifyPropertyChanged("DiodeVfEnabled"); }
        }

        double _DiodeVf;

        public double DiodeVf
        {
            get { return _DiodeVf; }
            set { _DiodeVf = value; NotifyPropertyChanged("DiodeVf"); }
        }
        bool _Diode2VfEnabled;

        public bool Diode2VfEnabled
        {
            get { return _Diode2VfEnabled; }
            set { _Diode2VfEnabled = value; NotifyPropertyChanged("Diode2VfEnabled"); }
        }

        double _Diode2Vf;

        public double Diode2Vf
        {
            get { return _Diode2Vf; }
            set { _Diode2Vf = value; NotifyPropertyChanged("Diode2Vf"); }
        }
        int _iDiv;

        public int IDiv
        {
            get { return _iDiv; }
            set { _iDiv = value; NotifyPropertyChanged("IDiv"); }
        }
        ObservableCollection<TolValue> _RtolValues;

        public ObservableCollection<TolValue> RtolValues
        {
            get { return _RtolValues; }
            set { _RtolValues = value; NotifyPropertyChanged("RtolValues"); }
        }
        TolValue _selectedRtolValue;

        public TolValue SelectedRtolValue
        {
            get { return _selectedRtolValue; }
            set {
                _selectedRtolValue = value; 
                Rtol = value.Value;
            }
        }
        double _Rtol;

        public double Rtol
        {
            get { return _Rtol; }
            set { _Rtol = value; NotifyPropertyChanged("Rtol"); }
        }
   
        ObservableCollection<TolValue> _CtolValues;

        public ObservableCollection<TolValue> CtolValues
        {
            get { return _CtolValues; }
            set { _CtolValues = value; NotifyPropertyChanged("CtolValues"); }
        }
        TolValue _selectedCtolValue;

        public TolValue SelectedCtolValue
        {
            get { return _selectedCtolValue; }
            set {
                _selectedCtolValue = value;
                NotifyPropertyChanged("SelectedCtolValue");
                Ctol = value.Value;
            }
        }
        double _Ctol;

        public double Ctol
        {
            get { return _Ctol; }
            set { _Ctol = value; NotifyPropertyChanged("Ctol"); }
        }

        public InputData()
        {
            string nameLocalized;
            WPFLocalizeExtension.Extensions.LocTextExtension locTextExt = new WPFLocalizeExtension.Extensions.LocTextExtension("MC34063_Universal_Calculator:Global:Andere");
            locTextExt.ResolveLocalizedValue(out nameLocalized);

            DefaultValues defVals = DefaultValuesController.Load(AppDomain.CurrentDomain.BaseDirectory + "DefaultValues.xml");
            
            _icValues = new ObservableCollection<ICValue>(defVals.ICValues);
            _diodeValues = new ObservableCollection<DiodeValue>(defVals.DiodeValues);
            _RtolValues = new ObservableCollection<TolValue>(defVals.RTolValues);
            _CtolValues = new ObservableCollection<TolValue>(defVals.CTolValues);

            DiodeValue defDiodeVal = new DiodeValue(nameLocalized, 0.4);
            ICValue defICVal = new ICValue(nameLocalized, nameLocalized, 40, 1.5, 1.25, 2.0, 0.7, 40, 0.7, 10000000, 0, 70);
            TolValue defRtolVal = new TolValue(nameLocalized, 1);
            TolValue defCtolVal = new TolValue(nameLocalized, 10);

            _icValues.Add(defICVal);
            _diodeValues.Add(defDiodeVal);
            _RtolValues.Add(defRtolVal);
            _CtolValues.Add(defCtolVal);


            var IsDefICVal = from i in _icValues
                                where i.IsDefault == true
                                select i;
            var IsDefCtolVal = from i in _CtolValues
                             where i.IsDefault == true
                             select i;
            var IsDefRtolVal = from i in _RtolValues
                             where i.IsDefault == true
                             select i;
            var IsDefDiodeVal = from i in _diodeValues
                             where i.IsDefault == true
                             select i;

            defICVal = (IsDefICVal.Count() > 0) ? IsDefICVal.First() : defICVal;
            defCtolVal = (IsDefCtolVal.Count() > 0) ? IsDefCtolVal.First() : defCtolVal;
            defRtolVal = (IsDefRtolVal.Count() > 0) ? IsDefRtolVal.First() : defRtolVal;
            defDiodeVal = (IsDefDiodeVal.Count() > 0) ? IsDefDiodeVal.First() : defDiodeVal;
            
            SelectedCtolValue = defCtolVal;
            SelectedDiodeValue = defDiodeVal;
            SelectedDiode2Value = defDiodeVal;
            SelectedICValue = defICVal;
            SelectedRtolValue = defRtolVal;


            Vinmin = Vinmax = 5;
            Vout = 12;
            Vsat2 = 0.8;
            Ioutmax = 0.1;
            Frequency = 50000;
            Vripplepp = 0.1;
            IDiv = 200;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string info)
        {
            if (info == "Vinmax" || info == "Vinmin" || info == "Vout")
            {
                if (Vout > Vinmin && Vout > Vinmax)
                {
                    Vsat = SelectedICValue.Vsat;
                }
                else
                {
                    Vsat = SelectedICValue.VsatDarlington;
                }
            }
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}

//(c) 2011 - Tobias Eiseler

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>